﻿using System;
using DataService.CrudClassGenerator;
using Types.Attributes;

namespace Services
{
    abstract partial class BusinessBase
    {
        #region [CLASS BASE MEMBERS] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        private bool mIsCcgRoot = false;
        public int nestedCcgLevel = 1;

        protected bool IsCcgRoot
        {
            get
            {
                if (nestedCcgLevel > 1)
                {
                    nestedCcgLevel--;
                    return false;
                }
                return mIsCcgRoot;
            }
        }

        #endregion

        #region [METHODS] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        CCGDataObjectAdoTran mCCGDataObject;

        protected CCGDataObjectAdoTran GetCCGDataObject(bool pUseAdoTranTransaction)
        {
            if (!pUseAdoTranTransaction) throw new NotSupportedException("This method can be used only for ADO transaction!");

            if (mCCGDataObject == null)
            {
                mCCGDataObject = new CCGDataObjectAdoTran(pGetDefaultDB());
                mIsRoot = true;
            }
            else
            {
                //Used to call more then one methods which are in same BS and each of them is in separate transaction
                if (mIsRoot)
                {
                    nestedLevel++;
                }
            }
            return mCCGDataObject;
        }

        protected CCGDataObject GetCCGDataObject()
        {
            return new CCGDataObject(pGetDefaultDB());
        }

        //If service has special connection information
        protected CCGDataObjectAdoTran GetCCGServiceDataObject()
        {
            string connectionStringName = string.Empty;

            if (this.GetType().IsDefined(typeof(ServiceConnectionNameAttribute), false))
            {
                ServiceConnectionNameAttribute cnAttribute = (ServiceConnectionNameAttribute)this.GetType().GetCustomAttributes(typeof(ServiceConnectionNameAttribute), false)[0];
                connectionStringName = cnAttribute.mConnectionName;
            }

            if (connectionStringName == string.Empty)
            {
                return GetCCGDataObject(true);
            }

            if (mCCGDataObject == null)
            {
                mCCGDataObject = new CCGDataObjectAdoTran(pGetDefaultDB(), connectionStringName);
                mIsCcgRoot = true;
            }
            else
            {
                if (mIsCcgRoot)
                {
                    nestedCcgLevel++;
                    return mCCGDataObject;
                }
                else if (!mIsCcgRoot)
                {
                    return mCCGDataObject;
                }
                else
                {
                    mCCGDataObject = new CCGDataObjectAdoTran(pGetDefaultDB(), connectionStringName);
                    mIsCcgRoot = true;
                }
            }
            return mCCGDataObject;
        }

        protected T CreateCCGBusinessObject<T>() where T : BusinessBase
        {
            T objBusiness = default(T);
            objBusiness = Activator.CreateInstance<T>();
            if (mCCGDataObject != null)
            {
                objBusiness.mCCGDataObject = this.mCCGDataObject;
                objBusiness.mIsRoot = false;
            }
            return objBusiness;
        }

        #endregion
    }
}
